$(function () {
    var uid = getParameter("uid");
    alert(uid)
    load();
    $.get("/music/user/findUserByUid", {uid: uid}, function (data) {
        $("#username").html(data.username);
        var collectedmusiclist=document.getElementById("collectedmusiclist")
        collectedmusiclist.href="sbcollections.html?uid="+uid;
        var createdmusiclist=document.getElementById("createdmusiclist")
        createdmusiclist.href="sbmusiclists.html?uid="+uid;

    });
});

function load(currentPage) {
    var mlid = getParameter("mlid");
    $.get("/music/music/findMusicInListByPage", {currentPage: currentPage, mlid: mlid}, function (data) {
        // console.log(data.list[0].musicList);
        console.log(data)
        if (data.totalCount === 0) {
            $("#errorMsg").html("歌单中还没有歌曲,<a href=" + mlid + "'../adminHtml/addMusicToList.html?mlid='>请去添加</a>")
        }
        if (data.list != null) {
            //获取tbody
            $tbody = $("#musicinlist");
            //拼接
            var trlist = '';
            for (var i in data.map) {
                var li= "<li>"+
                    '    <div className="songlist__songname">'+data.list[i].musicname+
                    '<div className="play">'+
                    '<img src="../images/play2.png" width=20px/>'+
                    '</div>'+
                    '</div>'+
                    '<div className="songlist__artist">'+data.map[i][1]+'</div>'+
                    '</li>'
                trlist += li;
            }
            //2.3 将liList添加到ul中
            $tbody.html(trlist);
            //4.处理页面 前5后4
            //4.1 开始页数 = 当前页数-5
            var start = data.currentPage - 5;
            //4.2结束页数=当前页数+4
            var end = data.currentPage + 4;
            //4.3判断前面不足5个的情况
            if (start < 1) {
                start = 1;
                end = 10;
            }
            //4.4判断后面不足4的情况
            if (end > data.totalPage) {
                end = data.totalPage;
                start = end - 9;
            }
            //4.5总页数不足10页
            if (data.totalPage < 10) {
                start = 1;
                end = data.totalPage;
            }
            //4.6拼接 ul -li
            $pageUl = $("#pageUl");
            //首页  cid = 3
            var pageList = '<li><a href="javaScript:load()">首页</a></li>';
            //上一页
            var prePage = data.currentPage - 1;
            if (prePage < 1) {
                prePage = 1;
            }
            pageList += '<li>' +
                '<a href="javaScript:load(' + prePage + ')">上一页</a></li>';
            //拼接 页码 从start开始 end 结束
            for (var i = start; i <= end; i++) {
                if (i === data.currentPage) {
                    pageList += '<li onclick="load(' + i + ')" class="active"><a href="javaScript:">' + i + '</a></li>';
                } else {
                    pageList += '<li onclick="load(' + i + ')" ><a href="javaScript:">' + i + '</a></li>';
                }
            }
            //下一页
            var nextPage = data.currentPage + 1;
            if (nextPage > data.totalPage) {
                nextPage = data.totalPage;
            }
            pageList += '<li onclick="load(' + nextPage + ')" ><a href="javascript:;">下一页</a></li>';
            //末页
            pageList += '<li onclick="load(' + data.totalPage + ')" ><a href="javascript:;">末页</a></li>';
            //4.7 将pageList添加到ul中
            $pageUl.html(pageList);
        }
    }, "json");
}

//移除歌单中的歌曲
function remMusicFromList(mid) {
    var mlid = getParameter("mlid");
    $.post("/music/musicList/remMusic", {mid: mid}, function (data) {
        alert("移除成功");
        location.href = "musicInList.html?mlid=" + mlid;
    });
}